package com.hean.activity;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.text.TextUtils;

import com.hean.util.DB;

public class ContactContentProvider extends ContentProvider{
	private DB db;
	private static final int ALLCONTACTS=1;
	private static final int CONTACT=2;
	private static final UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
	static{
		matcher.addURI("content://com.hean.provider.contactprovider", "t_contact", ALLCONTACTS);
		matcher.addURI("content://com.hean.provider.contactprovider", "t_contact/#", CONTACT);
	}
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase database=db.getWritableDatabase();
		int count=0;
		switch (matcher.match(uri)) {
		case ALLCONTACTS:
			count=database.delete("t_contact",selection,selectionArgs);
			break;
		case CONTACT:
			long id=ContentUris.parseId(uri);
			String where=TextUtils.isEmpty(selection)?"_id=?":selection+"and _id=?";
			String[] params=new String[]{String.valueOf(id)};
			if(!TextUtils.isEmpty(selection)&&selectionArgs!=null){
				params=new String[selectionArgs.length+1];
				System.arraycopy(selectionArgs, 0, params, 0, params.length);
				params[params.length-1]=String.valueOf(id);
			}
			count=database.delete("t_contact",where,params);
			break;
		default:
			throw new IllegalArgumentException("unknow uri:"+uri);
		}
		return count;
	}

	@Override
	public String getType(Uri uri) {
		switch (matcher.match(uri)) {
		case ALLCONTACTS:
			return "vnd.android.cursor.dir/contactprovider.contact";
		case CONTACT:
			return "vnd.android.cursor.item/contactprovider.contact";
		default:
			throw new IllegalArgumentException("unknow uri:"+uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase database=db.getWritableDatabase();
		System.out.println("ppppppppppppppppp");
		System.out.println(uri);
		long id=0;
		switch (matcher.match(uri)) {
		case ALLCONTACTS:
			id=database.insert("t_contact", "name", values);
			return ContentUris.withAppendedId(uri, id);
		case CONTACT:
			id=database.insert("t_contact", "name", values);
			String path=uri.toString();
			return Uri.parse(path.substring(0, path.lastIndexOf('/'))+id);//?????
		default:
			throw new IllegalArgumentException("unknow uri:"+uri);
		}
	}

	@Override
	public boolean onCreate() {
		db=new DB(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase database=db.getWritableDatabase();
		switch (matcher.match(uri)) {
		case ALLCONTACTS:
			return database.query("t_contact", projection, selection, selectionArgs, null, null, sortOrder);
		case CONTACT:
			long id=ContentUris.parseId(uri);
			String where=TextUtils.isEmpty(selection)?"_id=?":selection+"and _id=?";
			String[] params=new String[]{String.valueOf(id)};
			if(!TextUtils.isEmpty(selection)&&selectionArgs!=null){
				params=new String[selectionArgs.length+1];
				System.arraycopy(selectionArgs, 0, params, 0, params.length);
				params[params.length-1]=String.valueOf(id);
			}
			return database.query("t_contact", projection, where, params, null, null, sortOrder);
		default:
			throw new IllegalArgumentException("unknow uri:"+uri);
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase database=db.getWritableDatabase();
		int count=0;
		switch (matcher.match(uri)) {
		case ALLCONTACTS:
			count=database.update("t_contact", values,selection,selectionArgs);
			break;
		case CONTACT:
			long id=ContentUris.parseId(uri);
			String where=TextUtils.isEmpty(selection)?"_id=?":selection+"and _id=?";
			String[] params=new String[]{String.valueOf(id)};
			if(!TextUtils.isEmpty(selection)&&selectionArgs!=null){
				params=new String[selectionArgs.length+1];
				System.arraycopy(selectionArgs, 0, params, 0, params.length);
				params[params.length-1]=String.valueOf(id);
			}
			count=database.update("t_contact", values,where,params);
			break;
		default:
			throw new IllegalArgumentException("unknow uri:"+uri);
		}
		return count;
	}

}
